#include "TCPPoint.h"
#include "Message.h"

#include <iostream>
#include <iomanip>
using namespace std;

char buf[1024];
Connection con(-1);

void ReadComplete()
{
	buf[con.EndRead()] = 0;
	cout << "Read Complete: " << buf << "\n" ;
}

void MessageTest();

int main()
{
	/*TCPPoint server;
	if (server.Listen("",6789))
	{
		cout << "Listening...\n";
	}
	else
	{
		cout << "Error accurs: " << server.GetErrorMessage() << '\n';
		return -1;
	}

	while(true)
	{
		con = server.Accept();
		cout << "Connection info: \n"
			<< "sock: " << con._sock << '\n'
			<< con.GetLocalIp() << ' ' << con.GetLocalPort() << '\n'
			<< con.GetRemoteIp() << ' ' << con.GetRemotePort() << '\n';

		con.BeginRead(buf, 0, 1024, ReadComplete);
	}*/

	MessageTest();
}


void MessageTest()
{
	char buf[] = "Hello";

	Message msg(Message::TYPE_TEXT, buf, 0, 3456);

	char result[10240];
	int len = msg.GetFormatMessage(result, 0, 10240);


	cout << "Length: " << len << '\n';

	for (int i = 0; i < 7; ++i)
	{
		cout << hex << ((int)result[i] & 0xff) << '\n';
	}

	len = (result[3] & 0xff) | 
		  (result[4] & 0xff) << 8| 
		  (result[5] & 0xff) << 16 | 
		  (result[6] & 0xff) << 24;

	cout << dec << len;
}